# -*- coding: utf-8 -*-

import sys
from PySide import QtGui
from .EQSignal_UI_M import Ui_EQSignal_


def instance():
    return QtGui.QApplication.instance()

class Ui_EQSignal(QtGui.QMainWindow, Ui_EQSignal_):

    """Ui_EQSignal"""

    def __init__(self, parent=None):
        super(Ui_EQSignal, self).__init__(parent)
        self.setupUi(self)
        self.initViewTH()
        self.initViewSP()
        self.fileName = ""

    def initViewTH(self):
        self.graphicsViewTH.addPlot(0,0,title="Acceleration")
        self.graphicsViewTH.addPlot(1,0,title="Velocity")
        self.graphicsViewTH.addPlot(2,0,title="Displacement")

    def initViewSP(self):
        self.graphicsViewSP.addPlot(0,0,title="Spectra")
        self.graphicsViewSP.getItem(0,0).setLogMode(x=True)

    def plotTH(self, t, acc, vel, disp, cl=True):
        accItem = self.graphicsViewTH.getItem(0,0)
        if cl: accItem.clear()
        accItem.plot(t,acc)
        velItem = self.graphicsViewTH.getItem(1,0)
        if cl: velItem.clear()
        velItem.plot(t,vel)
        dispItem = self.graphicsViewTH.getItem(2,0)
        if cl: dispItem.clear()
        dispItem.plot(t,disp)

    def plotSP(self, SPT, SP, cl=True, TSP=[]):
        SPItem = self.graphicsViewSP.getItem(0,0)
        if cl: SPItem.clear()
        SPItem.plot(SPT,SP)
        if len(TSP) != 0: SPItem.plot(SPT,TSP)

    def getTXTFileName(self):
        fd = QtGui.QFileDialog()
        self.fileName = fd.getOpenFileName(self, "file", ".", "*.txt", "PEER Record File (*.txt *.TXT)")[0]

    def getNGAFileName(self):
        fd = QtGui.QFileDialog()
        self.fileName = fd.getOpenFileName(self, "file", ".", "*.at2", "PEER Record File (*.at2 *.AT2)")[0]

    def getParaSP(self):
        Zeta = [float(zi) for zi in self.DR.text().split(",")]
        Tstart = self.TS.value()
        Tstop  = self.TL.value()
        NT = self.NP.value()
        DM = self.DM.currentText()
        SM = self.SM.currentText()
        PD = self.PD.isChecked()
        return Zeta,Tstart,Tstop,NT,DM,SM,PD

def new_Ui_EQSignal():
    return Ui_EQSignal()

def test():
    import scipy as spy
    AppInstance = instance()
    if AppInstance == None:
        app = QtGui.QApplication(sys.argv)
    else:
        app = AppInstance
    
    x = spy.loadtxt

    ue = Ui_EQSignal()

    ue.show()
    app.exec_()
    # sys.exit(app.exec_())
